<?php

namespace App\Service;

use App\Models\CheckinRewardRuleModel;
use App\Models\CollectionModel;
use App\Models\FollowModel;
use App\Models\UserIntimacyModel;
use Carbon\Carbon;

class UserBehaveService
{
    public function isFollow($userId, $aiCharacterId): bool
    {
        $follow = FollowModel::getUserIdByCharacterId($userId, $aiCharacterId);
        return $follow && $follow['status'] == 1;
    }

    public function follow($userId, $aiCharacterId): bool
    {
        $follow = FollowModel::getUserIdByCharacterId($userId, $aiCharacterId);
        if (!$follow) {
            $params = [
                'user_id' => $userId,
                'character_id' => $aiCharacterId,
                'status' => 1,
                'handle_time' => Carbon::now()->toDateTimeString(),
            ];
            FollowModel::commit($params);
            return true;
        }
        if ($follow['status'] == 0) {
            $status = 1;
        } else {
            $status = 0;
        }
        $follow = FollowModel::updateStatus($follow['id'], $status);
        return $follow;
    }

    public function isCollection($userId, $businessId, $businessType = 1): bool
    {
        $collection = CollectionModel::getByBusiness($userId, $businessId, $businessType);
        return $collection && $collection['status'] == 1;
    }

    public function collection($userId, $businessId, $businessType = 1): bool
    {
        $follow = CollectionModel::getByBusiness($userId, $businessId, $businessType);
        if (!$follow) {
            $params = [
                'user_id' => $userId,
                'business_id' => $businessId,
                'business_type' => $businessType,
                'status' => 1,
                'handle_time' => Carbon::now()->toDateTimeString(),
            ];
            return CollectionModel::commit($params);
        }
        if ($follow['status'] == 0) {
            $status = 1;
        } else {
            $status = 0;
        }
        $collection = CollectionModel::updateStatus($follow['id'], $status);
        return $collection;
    }

    public function getUserIntimacy($initiatorId, $targetId)
    {
        $intimacy = UserIntimacyModel::getByInitiatorAndTarget($initiatorId, $targetId);
        return $intimacy && $intimacy['score'] ? $intimacy['score'] : 0;
    }

    public function checkInRule()
    {
        //  TODO
        CheckinRewardRuleModel::getRulesWithinTimePeriod();
    }
}
